/*
 AppJS v1.0
 */
angular.module('app', ['bsTable'])
.controller('MainController', function ($scope, $http) {
    $scope.workspaces = [];
    $scope.workspaces.push({ name: '待办任务',type:'taskList' });
    $scope.workspaces.push({ name: '完成任务',type:'finishedList' });

    function getData(type,colData) {
        var rows = [];
        var url="/declare/task/"+type;
        $http.get(url).success( function(data) {
            for (var i = 0; i < data.length; i++) {
                var task = data[i];
                // console.log(task.taskName);

                rows.push($.extend({index:i},colData,JsonToLowerCase(task)));
                console.log(JSON.stringify(rows));
            }
        });
        return rows;
    }

    $scope.workspaces.forEach(function (wk,index) {
        var type = wk.type;
        var colData = {workspace: wk.name};
        wk.rows = getData(type,colData);

        wk.bsTableControl = {
            options: {
                data: wk.rows,
                rowStyle: function (row, index) {
                    return { classes: 'none' };
                },
                cache: false,
                height: 400,
                striped: true,
                pagination: true,
                pageSize: 10,
                pageList: [5, 10, 25, 50, 100, 200],
                search: true,
                showColumns: true,
                showRefresh: false,
                minimumCountColumns: 2,
                clickToSelect: false,
                showToggle: true,
                maintainSelected: true,
                columns: [
                    {field: 'txnno',title: '流水号',align: 'center',valign: 'bottom',sortable: true},
                    {field: 'transorgno',title: '机构号',align: 'center',valign: 'bottom',sortable: true},
                    {field: 'curtbizno',title: '申报号码',align: 'center',valign: 'bottom',sortable: true},
                    {field: 'tradeno',title: '交易号',visible:false},
                    {field: 'tradename',title: '交易名称',align: 'center',valign: 'middle',sortable: true},
                    {field: 'transcur',title: '币种',align: 'left',valign: 'top',sortable: true},
                    {field: 'transamt',title: '金额',align: 'left',valign: 'top',sortable: true},
                    {field: 'transtate',title: '交易状态',align: 'center',valign: 'middle',clickToSelect: false,
                        formatter: com_formatter.transtate_formatter//, events: flagEvents
                    },
                    {field: 'systemdate',title: '创建日期',align: 'center',valign: 'middle',clickToSelect: false,
                        formatter: formatDate//, events: flagEvents
                    },

                    {field: 'memo',title: '备注',align: 'left',valign: 'top',sortable: true}],
                onDblClickRow : function(row, $element,field){
                    dblClickRow_TaskQuery(row, $element,field);
                }
            }
        };

    });

    $scope.changeCurrentWorkspace = function (wk) {
        $scope.currentWorkspace = wk;
    };

    $(document).ready(function () {
        $scope.changeCurrentWorkspace($scope.workspaces[0]);
        $scope.$apply();
    });

});

/**格式化对象*/
var com_formatter = {};
//交易状态格式化
com_formatter.transtate_formatter = function (value, row, index) {
   return $decCom.constants.getCstVal("transtate",value);
}

/**任务列表双击查询事件**/
function dblClickRow_TaskQuery(row, $element,field){
    row.tradename = "测试交易";
    // var url = "/declare/" + row.tradename + "/" + row.txnno;
    var url = "/declare/bopb/edit/0";
    var jon = {
        pageId : "content-page",contentId : "content-main",txnno:row.txnno,
        url : url,name : row.tradename||row.tradeno
    }
    $decCom.bootsTab.addTab(jon);
    // alert(url);
}